/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication5.model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
import org.omg.CORBA.DATA_CONVERSION;

/**
 *
 * @author mathieubakrim
 */
public class AddressBookModel {
    private ContactsTable contactsTable;
    
    private ArrayList<Contact> contactsList = new ArrayList<Contact>();
    private String [] group = {"Friends", "family", "companies", "co-workers"};
    
    public AddressBookModel(){
        //createContact("test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "Test9");
        try {
            csvReader();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AddressBookModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AddressBookModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    
    
    public void csvReader() throws FileNotFoundException, IOException{
        BufferedReader reader = new BufferedReader(new FileReader("data1.csv"));
        String csvContacts = null;
        
        reader.readLine(); // avoid to retrieve the title line as a contact
        
        while((csvContacts = reader.readLine()) != null){
            
            String[] contactInfoData = csvContacts.split(";");
            
            final SimpleContact contact = new SimpleContact();
                
            contact.setLastName(contactInfoData[0]);
            contact.setFirstName(contactInfoData[1]);
            contact.setPhoneNumber(contactInfoData[2]);
            contact.setAddressPro(contactInfoData[3]);
            contact.setAddressPer(contactInfoData[4]);
            contact.setHomePage(contactInfoData[5]);
            contact.setEmailPro(contactInfoData[6]);
            contact.setEmailPer(contactInfoData[7]);
            contact.setGroup(contactInfoData[8]);
            
            contactsList.add(contact);
        }
    }
    
    public void csvWriter() throws IOException{
        char separator = ';';
        BufferedWriter writer = new BufferedWriter(new FileWriter("data1.csv"));
        for(int i = 0; i< contactsTable.getTitles().length; i++){
            writer.write(contactsTable.getTitles()[i] + separator);
        }
        writer.newLine();
        
        for(int i = 0; i < contactsList.size(); i++){
            writer.write(contactsList.get(i).getLastName()+ separator);
            writer.write(contactsList.get(i).getFirstName()+ separator);
            writer.write(contactsList.get(i).getPhoneNumber()+ separator);
            writer.write(contactsList.get(i).getAddressPro()+ separator);
            writer.write(contactsList.get(i).getAddressPer()+ separator);
            writer.write(contactsList.get(i).getHomePage() + separator);
            writer.write(contactsList.get(i).getEmailPro() + separator);
            writer.write(contactsList.get(i).getEmailPer()+ separator);
            writer.write(contactsList.get(i).getGroup()+ separator);
            writer.newLine();
        }
        
        writer.close();
        
    }
    

    public String[] getGroup() {
        return group;
    }
    
    
    
    /**
     * Getter
     * @return List of contacts actually saved in memory
     **/
    public ArrayList<Contact> getContactsList() {
        return contactsList;
    }
    
    public void setContactsList(List<Contact> contactsList) {
        this.contactsList = (ArrayList<Contact>)contactsList;
    }
    
    
/**
     * Create a contact with datas read from the controller
     * @param in: array of the Createform's textfields
     * @param out: an Object Contact, saved in model arraylist
     **/
     public Contact createContact(String ln, String fn, String pn, String ap, String ape, String hp, String ep, String epe, String gr) {
            
         Contact c = new SimpleContact(ln, fn, pn, ap, ape, hp, ep, epe, gr);
       
         contactsList.add(c);
         System.out.println("List: " + getContactsList().toString());
         return c;
     }  
    /**
     * Delete a contact
     * @Todo : destroy the object after removing it from the list
     **/
    public void deleteContact(Contact c){
        contactsList.remove(c);
    }

    public ContactsTable createContactTable(String[] tab_string, ArrayList<Contact> contacts) {
        contactsTable = new ContactsTable(tab_string, contacts);
        return contactsTable;
    }
    
}


